package servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Utilisateur;
import dao.DaoUtilisateur;

/**
 * Servlet implementation class Inscription
 */
public class Inscription extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Inscription() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		DaoUtilisateur daouser = new DaoUtilisateur();
				
		if(!daouser.checkExistLogin(request.getParameter("login"))){
			request.setAttribute( "login_error", "1");
	        request.getRequestDispatcher( "/inscription.jsp").forward( request, response );
		}else{
			
		Utilisateur user = new Utilisateur();
		user.setNomUtilisateur(request.getParameter("nom"));
		user.setPrenomUtilisateur(request.getParameter("prenom"));
		user.setRue(request.getParameter("num_rue"));
		user.setNomrue(request.getParameter("nom_rue"));
		user.setVille(request.getParameter("ville"));
		user.setCodepostal(Integer.valueOf(request.getParameter("cp")));
		user.setLogin(request.getParameter("login"));
		user.setPassword(request.getParameter("pwd"));
		user.setType("U");
		user.setEmail(request.getParameter("email"));
		
		daouser.create(user);
		
		request.setAttribute( "login_error", "0");
		request.getRequestDispatcher( "/inscription.jsp").forward( request, response );
		}
		
	}

}
